#!/usr/bin/env python
# -*- coding: utf-8 -*-


from .rpc import RPC
from .env import Env
from .addons import models

from .odoorpc import Env as OdooEnv


class ODOO(object):
    def __init__(self, host, db):
        self.rpc = RPC(host, db)
        self.env = Env(self.rpc, models)
        self.odooenv = OdooEnv(self.rpc)

        self.addons = models

    @property
    def session(self):
        return self.rpc.session

    @property
    def version(self):
        return self.rpc.version

    def login(self, user, password):
        session = self.rpc.authenticate(user, password)
        user_companies = self.session['user_companies']
        allowed_companies = user_companies['allowed_companies']
        allowed_company_ids = map(lambda c: c[0], allowed_companies)
        self.env.allowed_company_id = min(allowed_company_ids)
        # self.env.allowed_company_id = max(allowed_company_ids)

        return session

        # 这个值 要写到 localstore 里.
        # 刷新页面时,
        # 读 sessionid, 重新 从服务器取 session info,
        # 且 从 localstore 取 allowed_company_id, 写到 api 里

    # def get_session_info(self, token=None):
    #     return self.rpc.get_session_info(token)


def get_odoo(host=None, db=None, user=None, password=None, models=None, version=None):
    pass

    # host = options.get('host')
    # db = options.get('db')
    # user = options.get('user')
    # password = options.get('password')

    # odoo = ODOO(host, db, models, version)
    # sid = odoo.login(user, password)

    # return sid and odoo


if __name__ == '__main__':
    pass

    host = ''
    db = ''
    user = ''
    password = ''

    # odoo = ODOO(host, db)
    # odoo.login(user, password)
